package leetcode.datastructure.str;

public class Program58LastWordLength {

    public Program58LastWordLength() {
    }

    public int lengthOfLastWord(String s) {
        int length = s.length();
        int right = length - 1;
        while (right >= 0 && s.charAt(right) == ' ') {
            right--;
        }
        if (right < 0) {
            return 0;
        }
        int left = right - 1;
        while (left >= 0 && s.charAt(left) != ' ') {
            left--;
        }
        return right - left;
    }

    public static void main(String[] args) {
        Program58LastWordLength obj = new Program58LastWordLength();
//        System.out.println(obj.lengthOfLastWord(""));
//        System.out.println(obj.lengthOfLastWord("Hello World"));
//        System.out.println(obj.lengthOfLastWord("   fly me   to   the moon  "));
//        System.out.println(obj.lengthOfLastWord("luffy is still joyboy"));
        System.out.println(obj.lengthOfLastWord("day"));

    }
}
